package com.ruoyi.central.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.cache.common.CacheConstants;
import com.ruoyi.central.domain.TopCentralArea;
import com.ruoyi.central.enums.AreaTypeEnum;
import com.ruoyi.central.service.ITopCentralAreaService;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.bean.EnumUtils;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.service.ServiceImpl;
import com.ruoyi.central.mapper.TopCentralOperationStationMapper;
import com.ruoyi.central.domain.TopCentralOperationStation;
import com.ruoyi.central.service.ITopCentralOperationStationService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.enums.common.State;
import java.util.Collection;
import java.util.ArrayList;

/**
 * 操作工位Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-02
 */
@Service
@Slf4j
@CacheConfig(cacheNames = CacheConstants.EhcacheName.CONTINUAL_CACHE_NAME)
public class TopCentralOperationStationServiceImpl extends ServiceImpl<TopCentralOperationStationMapper,TopCentralOperationStation> implements ITopCentralOperationStationService
{
    @Autowired
    private ITopCentralAreaService areaService;
    /**
     * 查询操作工位
     * 
     * @param id 操作工位主键
     * @param columns 查询指定的列
     * @return 操作工位
     */
    @Override
    public TopCentralOperationStation selectById(Integer id, SFunction<TopCentralOperationStation,?> ... columns){

        if(StringUtils.isNotEmpty(columns)){
            return selectOne(lambdaQuery().select(columns).eq(TopCentralOperationStation::getId,id));
        }
        return super.selectById(id);
    }

    /**
     * 查询操作工位
         * 注意：只获取状态信息为可用的数据
     * 
     * @param id 操作工位主键
     * @param columns 查询指定的列
     * @return 操作工位
     */
    @Override
    public TopCentralOperationStation selectUsableById(Integer id, SFunction<TopCentralOperationStation,?> ... columns)
    {
            LambdaQueryWrapper<TopCentralOperationStation> lambdaQueryWrapper = lambdaQuery().eq(TopCentralOperationStation::getId,id);
            buildUsableState(lambdaQueryWrapper);
            if(StringUtils.isNotEmpty(columns)){
                lambdaQueryWrapper.select(columns);
            }
            return selectOne(lambdaQueryWrapper);
    }

    /**
     * 查询操作工位列表
     *
     * @param topCentralOperationStation 操作工位
     * @return 操作工位
     */
    @Override
    public List<TopCentralOperationStation> selectList(TopCentralOperationStation topCentralOperationStation)
    {
        return getBaseMapper().selectTopCentralOperationStationList(topCentralOperationStation);
    }

    /**
     * 获取自动区域的操作工位集合
     * @return
     */
    @Override
    public List<TopCentralOperationStation> getAutoOperationStationList() {
        //获取自动的操作区域编号集合
        List<Integer> areaListOfAutomatic = areaService.selectAllMap().values().stream()
                .filter(item-> EnumUtils.equals(AreaTypeEnum.AUTOMATIC_OPERATION,item.getType()))
                .map(TopCentralArea::getId).collect(Collectors.toList());
        if(StringUtils.isNotEmpty(areaListOfAutomatic)){
            LambdaQueryWrapper<TopCentralOperationStation> lambdaQueryWrapper = customLambdaQuery();
            if(CollectionUtils.isSingle(areaListOfAutomatic)){
                lambdaQueryWrapper.eq(TopCentralOperationStation::getAreaId,CollectionUtils.getFirst(areaListOfAutomatic));
            }else{
                lambdaQueryWrapper.in(TopCentralOperationStation::getAreaId,areaListOfAutomatic);
            }
            return getBaseMapper().selectList(lambdaQueryWrapper);
        }

       return CollectionUtils.EMPTY_LIST;
    }

    /**
     * 查询简单的实体列表数据
     * @param topCentralOperationStation
     * @param columns 查询指定列
     * @return
     */
    @Cacheable
    @Override
    public List<TopCentralOperationStation> selectSimpleList(TopCentralOperationStation topCentralOperationStation, SFunction<TopCentralOperationStation,?>... columns){
        return super.selectSimpleList(topCentralOperationStation,columns);
    }
    /**
     * 获取所有的操作工位数据
     * 
     * @return Map<Integer,TopCentralOperationStation> 集合
     */
    @Cacheable
    @Override
    public Map<Integer,TopCentralOperationStation> selectAllMap(){

        return getBaseMapper().selectList(customLambdaQuery())
                .stream().collect(Collectors.toMap(TopCentralOperationStation::getId,v->v));
    }

    /**
     * 新增操作工位
     *
     * @param topCentralOperationStation 操作工位
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean insert(TopCentralOperationStation topCentralOperationStation)
    {
        return super.insert(topCentralOperationStation);
    }

    /**
     * 新增（批量）操作工位
     *
     * @param topCentralOperationStationList 实体对象集合
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean insertBatch(Collection<TopCentralOperationStation> topCentralOperationStationList, int batchSize) {
        return super.insertBatch(topCentralOperationStationList, batchSize);
    }

    /**
     * 删除 操作工位 根据编号
     * @param id
     * @return true 删除成功 false 删除失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }

    /**
     * 批量删除 操作工位 根据 编号集合
     * @param ids
     * @return true 删除成功 false 删除失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean deleteByIds(Collection<? extends Serializable> ids) {
        return super.deleteByIds(ids);
    }

    /**
     * 批量修改 操作工位 根据唯一编号
     * @param topCentralOperationStationList
     * @param batchSize  更新批次数量
     * @return true 修改成功 false 修改失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateBatchById(Collection<TopCentralOperationStation> topCentralOperationStationList, int batchSize) {
        return super.updateBatchById(topCentralOperationStationList, batchSize);
    }

    /**
     * 修改 操作工位
     * @param  topCentralOperationStation
     * @param updateWrapper
     * @return
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean update(TopCentralOperationStation topCentralOperationStation, LambdaUpdateWrapper<TopCentralOperationStation> updateWrapper) {
        return super.update(topCentralOperationStation,updateWrapper);
    }

    /**
     * 修改操作工位
     *
     * @param topCentralOperationStation 操作工位
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateById(TopCentralOperationStation topCentralOperationStation)
    {
        return super.updateById(topCentralOperationStation);
    }

    /**
     * 修改操作工位状态 根据 id 操作工位
     * 
     * @param id 操作工位主键
     * @param state 数据状态
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateStateById(Integer id, State state){

        return updateById(buildTopCentralOperationStationForUpdateState(id,state));
    }

    /**
     * 修改多个操作工位状态 根据 id 操作工位
     * 
     * @param ids 操作工位主键
     * @param state 数据状态
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateStateByIds(Collection<Integer> ids, State state){

        List<TopCentralOperationStation> updateTopCentralOperationStationList = new ArrayList(ids.size());

            ids.forEach(id->updateTopCentralOperationStationList.add(buildTopCentralOperationStationForUpdateState(id,state)));

        return updateBatchById(updateTopCentralOperationStationList);
    }

    /**
     * 构建修改TopCentralOperationStation 的 修改类
     * @param id 操作工位主键
     * @param state 数据状态
     * @return TopCentralOperationStation构建的修改模型
     */
    private TopCentralOperationStation buildTopCentralOperationStationForUpdateState(Integer id, State state){

        TopCentralOperationStation topCentralOperationStation = new TopCentralOperationStation();
        topCentralOperationStation.setState(state);
        topCentralOperationStation.setId(id);

        return topCentralOperationStation;
    }

}
